package main

import (
	"fmt"
	"math/rand"
	"sync"
	"time"
)

//通道得发送和接收特性

// 创建int类型通道，只能传入int类型值
var values = make(chan int)

func send() {
	rand.Seed(time.Now().UnixNano())
	value := rand.Intn(10)

	fmt.Println("--------", value)
	values <- value
}

func main() {
	//从通道接受值
	defer close(values)
	go send()
	fmt.Println("wait-----")
	value := <-values
	fmt.Println("rrrrrrrrrr", value)
	fmt.Println("end-----------")
	for i := 0; i < 10; i++ {
		wg.Add(1) // 启动一个goroutine就登记+1
		go hello(i)
	}
	wg.Wait() // 等待所有登记的goroutine都结束
}

var wg sync.WaitGroup

func hello(i int) {
	defer wg.Done() // goroutine结束就登记-1
	fmt.Println("Hello Goroutine!", i)
}
